package com.ogawa.joypal.distributionnetwork.widget;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ogawa.joypal.distributionnetwork.R;

import java.util.List;

/**
 * 作者: bright.lin@medisana.net.cn
 * 时间: 2018/12/18
 * 标题: 自定义下拉菜单列表适配器
 * 描述: 基于PopupWindow的下拉菜单中RecyclerView的适配器
 * 修改: 由 bright.lin@medisana.net.cn 修改于 2018/12/18
 */
public class SoftApPopwindowAdapter extends RecyclerView.Adapter<SoftApPopwindowAdapter.MyHolder> {

    // ScanResult对象列表
    private List<ScanResult> data;
    // 单个选项点击事件回调
    private OnItemClickListener onItemClickListener;

    /**
     * 带参构造器
     *
     * @param data ScanResult对象列表
     */
    SoftApPopwindowAdapter(List<ScanResult> data) {
        this.data = data;
    }

    /**
     * 更新数据
     *
     * @param data ScanResult对象列表
     */
    void update(List<ScanResult> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 设置单个选项点击事件回调
     *
     * @param onItemClickListener 单个选项点击事件回调
     */
    void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * @see RecyclerView.Adapter#onCreateViewHolder(ViewGroup, int)
     */
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_softap_popwindow_item, parent, false);
        return new MyHolder(view);
    }

    /**
     * @see RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int)
     */
    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, int position) {
        ScanResult scanResult = data.get(position);
        holder.textView.setText(scanResult.SSID);
        int signalLevel = WifiManager.calculateSignalLevel(scanResult.level, 100);
        holder.strengthTv.setText("" + signalLevel + "%");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener) {
                    onItemClickListener.onItemClick(data.get(holder.getAdapterPosition()));
                }
            }
        });
    }

    /**
     * @see RecyclerView.Adapter#getItemCount()
     */
    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 单个选项点击事件回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(ScanResult scanResult);
    }

    /**
     * 自定义ViewHolder
     */
    class MyHolder extends RecyclerView.ViewHolder {
        TextView textView;
        TextView strengthTv;

        MyHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.softap_popwindow_item_name);
            strengthTv = itemView.findViewById(R.id.softap_popwindow_item_strength);
        }
    }




}
